﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XnaColor = Microsoft.Xna.Framework.Color;
using texture2d = Microsoft.Xna.Framework.Graphics.Texture2D;
using XnaRect = Microsoft.Xna.Framework.Rectangle;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using WinColor = System.Drawing.Color;
using WinPoint = System.Drawing.Point;

namespace JYoperation.Class
{
    public class JYEvents
    {
        public class UIsurface
        {
            public int IntIsworkingState = -1;
            public string strIsworkingMenu = null;
            public List<UIsurfaceType> UIlist;
            public enum UiKeys
            {
                UiUp = 0,
                UiDown = 1,
            }
            public class UIsurfaceType
            {
                //0-菜单，1-字符串
                public int state;
                public string strDisPlay;
                public WinColor strColorBack;
                public WinColor strColorFront;
                public XnaRect rect;
                public string Tag;
                public UIMenuType UIMenu;
                //texture
                public Texture2D texture;
                public WinPoint Pointtexture;
                public float alpha;
                public bool Visible = true;
            }
            public class UIMenuType
            {
                public string MenuTitle;
                public UIMenuStringType[] MenuString;
                public int SelectIndex;
                public int x;
                public int y;
                public string LastTag;
            }
            public class UIMenuStringType
            {
                public string UiMenuString;
                public EventHandler UiMenuEvents;
            }
            public UIsurface()
            {
                UIlist = new List<UIsurfaceType>();
            }
            
            public void MoveMenu(UiKeys Uikey)
            {
                MoveMenu(strIsworkingMenu, Uikey);
            }
            public void SetVisible(string strTag)
            {
                for (int i = 0; i < UIlist.Count; i++)
                {
                    UIsurfaceType tmpUI = UIlist[i];
                    if (tmpUI.Tag.Equals(strTag))
                    {
                        tmpUI.Visible = !tmpUI.Visible;
                        //break;
                    }
                }
            }
            public void DoMenuEvents(string strTag)
            {
                                //foreach (UIsurfaceType tmpUI in UIlist)
                for (int i = 0; i < UIlist.Count; i++)
                {
                    UIsurfaceType tmpUI = UIlist[i];
                    if (tmpUI.Tag.Equals(strTag) && tmpUI.state == 2)
                    {
                        int index = tmpUI.UIMenu.SelectIndex;
                        //do
                        tmpUI.UIMenu.MenuString[index].UiMenuEvents(null, null);
                        break;
                    }
                }
            }
            /// <summary>
            /// 获取选项
            /// </summary>
            /// <returns></returns>
            public int GetMenuIndex()
            {
                foreach (UIsurfaceType tmpUI in UIlist)
                {
                    //UIsurfaceType tmpUI = UIlist[i];
                    if (tmpUI.Tag.Equals(strIsworkingMenu) && tmpUI.state == 2)
                    {
                        return tmpUI.UIMenu.SelectIndex;
                    }
                }
                return -1;
            }
            /// <summary>
            /// 获取选项
            /// </summary>
            /// <returns></returns>
            public int GetMenuIndex(string strTag)
            {
                foreach (UIsurfaceType tmpUI in UIlist)
                {
                    //UIsurfaceType tmpUI = UIlist[i];
                    if (tmpUI.Tag.Equals(strTag) && tmpUI.state == 2)
                    {
                        return tmpUI.UIMenu.SelectIndex;
                    }
                }
                return -1;
            }
            public void MoveMenu(string strTag,UiKeys Uikey)
            {
                //foreach (UIsurfaceType tmpUI in UIlist)
                for(int i = 0;i<UIlist.Count;i++)
                {
                    UIsurfaceType tmpUI = UIlist[i];
                    if (tmpUI.Tag.Equals(strTag) && tmpUI.state == 2)
                    {
                        int index = tmpUI.UIMenu.SelectIndex;
                        int count = tmpUI.UIMenu.MenuString.Length;
                        switch (Uikey)
                        {
                            case UiKeys.UiUp:
                                if (index == 0)
                                    tmpUI.UIMenu.SelectIndex = count - 1;
                                else
                                    tmpUI.UIMenu.SelectIndex--;
                                break;
                            case UiKeys.UiDown:
                                if (index == count - 1)
                                    tmpUI.UIMenu.SelectIndex = 0;
                                else
                                    tmpUI.UIMenu.SelectIndex++;
                                break;
                        }
                        UIlist[i] = tmpUI;
                        return;
                    }
                }
            }
            /// <summary>
            /// 增加字符串
            /// </summary>
            /// <param name="str"></param>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <param name="colorBack"></param>
            /// <param name="colorFront"></param>
            public void addString(string strTag,string str, int x, int y, WinColor colorBack, WinColor colorFront)
            {
                UIsurfaceType tmpUI = new UIsurfaceType();
                tmpUI.UIMenu = new UIMenuType();
                tmpUI.state = 1;
                tmpUI.strDisPlay = str;
                tmpUI.strColorBack = colorBack;
                tmpUI.strColorFront = colorFront;

                //读取貌似只读取第一个符合tag的值
                if (!existsTag(strTag))
                    tmpUI.UIMenu.LastTag = strIsworkingMenu;
                else
                    tmpUI.UIMenu.LastTag = null;
                tmpUI.Tag = strTag;
                tmpUI.rect = new XnaRect(x, y, 0, 0);
                UIlist.Add(tmpUI);
                IntIsworkingState = tmpUI.state;
            }
            public void addTexture(string strTag,Texture2D texture,int x,int y,float alpha)
            {
                UIsurfaceType tmpUI = new UIsurfaceType();
                tmpUI.UIMenu = new UIMenuType();
                tmpUI.state = 3;
                tmpUI.texture = texture;

                //读取貌似只读取第一个符合tag的值
                if (!existsTag(strTag))
                    tmpUI.UIMenu.LastTag = strIsworkingMenu;
                else
                    tmpUI.UIMenu.LastTag = null;
                tmpUI.Tag = strTag;
                tmpUI.alpha = alpha;
                tmpUI.Pointtexture = new WinPoint(x, y);
                IntIsworkingState = tmpUI.state;
                UIlist.Add(tmpUI);
            }
            /// <summary>
            /// 增加菜单
            /// </summary>
            /// <param name="strTag"></param>
            /// <param name="strTitle"></param>
            /// <param name="strMenu"></param>
            /// <param name="Event"></param>
            /// <param name="x"></param>
            /// <param name="y"></param>
            public void addMenu(string strTag, string strTitle, string[] strMenu,EventHandler[] Event, int x,int y)
            {
                UIMenuType tmpmenu = new UIMenuType();

                tmpmenu.MenuString = new UIMenuStringType[strMenu.Length];
                for (int i = 0; i < strMenu.Length; i++)
                {
                    tmpmenu.MenuString[i] = new UIMenuStringType();
                    tmpmenu.MenuString[i].UiMenuString = strMenu[i];
                    if (Event.Length > i)
                        tmpmenu.MenuString[i].UiMenuEvents += new EventHandler(Event[i]);
                    else
                        tmpmenu.MenuString[i].UiMenuEvents += new EventHandler(Event[Event.Length - 1]);
                }
                tmpmenu.MenuTitle = strTitle;
                tmpmenu.x = x;
                tmpmenu.y = y;
                if (!existsTag(strTag))
                    tmpmenu.LastTag = strIsworkingMenu;
                else
                    tmpmenu.LastTag = null;
                UIsurfaceType tmpui = new UIsurfaceType();
                tmpui.Tag = strTag;
                tmpui.state = 2;
                tmpui.UIMenu = tmpmenu;
                IntIsworkingState = tmpui.state;
                UIlist.Add(tmpui);

                strIsworkingMenu = strTag;
            }
            public void addString(string strTag,string str, int x, int y)
            {
                addString(strTag, str,  x,  y,WinColor.Brown, WinColor.Orange);
            }
            public void addString(string strTag, string str, int x, int y,WinColor color)
            {
                addString(strTag, str, x, y, WinColor.White,color);
            }
            /// <summary>
            /// 增加矩形框(选定上一个返回标签)
            /// </summary>
            /// <param name="rect"></param>
            public void addRect(string strTag, string LastTag,XnaRect rect)
            {
                UIsurfaceType tmpUI = new UIsurfaceType();
                tmpUI.UIMenu = new UIMenuType();
                tmpUI.state = 0;
                tmpUI.rect = rect;
                tmpUI.Tag = strTag;
                //读取貌似只读取第一个符合tag的值
                if (!existsTag(strTag))
                    tmpUI.UIMenu.LastTag = LastTag;
                else
                    tmpUI.UIMenu.LastTag = null;
                strIsworkingMenu = strTag;
                UIlist.Add(tmpUI);
                IntIsworkingState = tmpUI.state;
            }
            /// <summary>
            /// 增加矩形框
            /// </summary>
            /// <param name="rect"></param>
            public void addRect(string strTag,XnaRect rect)
            {
                UIsurfaceType tmpUI = new UIsurfaceType();
                tmpUI.UIMenu = new UIMenuType();
                tmpUI.state = 0;
                tmpUI.rect = rect;
                tmpUI.Tag = strTag;
                //读取貌似只读取第一个符合tag的值
                if (!existsTag(strTag))
                    tmpUI.UIMenu.LastTag = strIsworkingMenu;
                else
                    tmpUI.UIMenu.LastTag = null;
                strIsworkingMenu = strTag;
                UIlist.Add(tmpUI);
                IntIsworkingState = tmpUI.state;
            }
            /// <summary>
            /// 获取当前标签状态
            /// </summary>
            /// <param name="strTag"></param>
            /// <returns></returns>
            private int GetState(string strTag)
            {
                foreach (UIsurfaceType tmpUI in UIlist)
                {
                    if (tmpUI.Tag.Equals(strTag))
                    {
                        return tmpUI.state;
                    }
                }
                return -1;
            }
            /// <summary>
            /// 根据标签删除条目
            /// </summary>
            /// <param name="strTag"></param>
            public void RemoveByTag(string strTag)
            {
                //关于转换tag的
                bool isDel = false;
                for(int i = 0;i<UIlist.Count;i++)
                {
                    if (UIlist[i].Tag.Equals(strTag))
                    {
                        if (!isDel)
                        {
                            strIsworkingMenu = UIlist[i].UIMenu.LastTag;
                            IntIsworkingState = GetState(strIsworkingMenu);
                            isDel = !isDel;
                        }
                        UIlist.RemoveAt(i);
                        i--;
                    }
                }
            }
            public bool existsTag(string strTag)
            {
                foreach (UIsurfaceType tmpUI in UIlist)
                {
                    if(tmpUI.Tag.Equals(strTag))
                    {
                        return true;
                    }
                }
                return false;
            }
        }
    }
}
